<?php

namespace app\index\controller;

use think\exception\ValidateException;
use app\model\Order;
use app\model\OrderGoods;
use app\model\Member;
use app\model\Comment;

class CommentController extends Base
{
    public function add()
    {
        $Membermob = new Member;
        $memberinfo = $Membermob->getUserByWechat();

        $postJsonString = input("post.postJsonString");
        $postdata = json_decode($postJsonString, true);

        foreach ($postdata as $vo) {
            Comment::create([
                'weid' => weid(),
                'uid' => $memberinfo['id'],
                'nick_name' => $memberinfo['nickname'],
                'head_img_url' => $memberinfo['userpic'],
                'status' => 0,
                'order_id' => $vo['order_id'],
                'goods_id' => $vo['goods_id'],
                'level' => $vo['level'],
                'content' => $vo['content'],
                'create_time' => time(),
                'update_time' => time()
            ]);
        }

        Order::update(['is_comment' => 1, 'id' => $postdata[0]['order_id']]);
        return $this->json(['msg'=>'感谢您的评价','data' => $data]);
    }

    public function getgoodscomment()
    {

        $goods_id = input("get.goodsId");
        $where['goods_id'] = $goods_id;
        $where['status'] = 1;
        $data =    Comment::where($where)->order('id asc')->select()->toArray();
        foreach ($data as &$vo) {
            $vo['nick_name'] = substr_cut($vo['nick_name']);
        }
        return $this->json(['data' => $data]);
    }

    public function getgoods()
    {

        $goods_id = input("get.goodsId");
        $OrderGoods = new OrderGoods;

        $data=   $OrderGoods->getOrderGoods($goods_id);


        foreach ($data as &$vo) {
            $vo['level'] = 5;
            $vo['content'] = '';  
        }
        return $this->json(['data' => $data]);
    }
}
